package com.example.mq.server.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.server.core.Binding;
import com.example.mq.server.core.Exchange;
import com.example.mq.server.core.ExchangeType;
import com.example.mq.server.core.MSGQueue;
import com.example.mq.server.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/*
* 通过这个类来整合上述数据库的操作
* */
@Slf4j
public class DataBaseManager {

    private MetaMapper metaMapper;

    /**
     * 数据库初始化操作
     */
    public void init(){
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if(!checkDBExist()){
            // 数据库不存在,就进行建库建表操作
            File dataDir = new File("./data");
            dataDir.mkdirs();
            // 创建数据表
            createTable();
            // 插入默认数据
            createDefaultData();
            log.info("数据库初始化完成!");
        }else{
            log.info("数据库已经存在!");
        }
    }

    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean delete = file.delete();
        if(delete){
            log.info("删除数据库成功!");
        }else{
            log.info("删除数据库失败!");
        }
        File dataDir = new File("./data");
        boolean delete1 = dataDir.delete();
        if(delete1){
            log.info("data目录删除成功!");
        }else{
            log.info("data目录删除失败!");
        }
    }

    /**
     * 判定是否存在 查看数据库所在路径下的文件是否存在 ./data/meta.db
     * @return
     */
    private boolean checkDBExist() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBingingTable();
        log.info("创建表完成!");
    }


    /**
     * 添加一个默认的交换机 默认是DIRECT的匿名交换机
     */
    private void createDefaultData() {
        // 构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);

        metaMapper.insertExchange(exchange);
        log.info("创建默认交换机完成!");
    }



    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }



    public void insertQueue(MSGQueue msgQueue){
        metaMapper.insertQueue(msgQueue);
    }

    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }



    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }

}
